package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class OrdersServiceImpl implements OrdersService {


    @Autowired(required = false)
    private OrdersMapper ordersMapper;
    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;
    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;
    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired(required = false)
    private UserMapper userMapper;
    /**
     * 添加订单
     *
     * @param orders
     * @return
     */
    //多条语句，开启事务
    @Transactional
    @Override
    public void addOrders(Orders orders,Long userId) {
        //A. 获得当前用户id, 查询当前用户的购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findShoppingCartListByUserId(userId);
        //B. 根据当前登录用户id, 查询用户数据
        User user = userMapper.findUserById(userId);
        //C. 根据地址ID, 查询地址数据
        AddressBook address = addressBookMapper.findAddressById(orders.getAddressBookId());
        //D. 组装订单明细数据, 批量保存订单明细
        Long orderId=UUIDUtils.getUUIDInOrderId().longValue();
        //声明变量记录总金额
        BigDecimal totalAmount = new BigDecimal("0");

        ArrayList<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(UUIDUtils.getUUIDInOrderId().longValue());
            orderDetail.setName(shoppingCart.getName());//购物项的菜品名字
            orderDetail.setOrderId(orderId);
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());//数量
            orderDetail.setAmount(shoppingCart.getAmount());//单价
            orderDetail.setImage(shoppingCart.getImage());//图片

            //计算每一个购物项的总价
            BigDecimal amount = shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber()));
            totalAmount=totalAmount.add(amount);
            orderDetailList.add(orderDetail);
        }
        //E. 组装订单数据, 批量保存订单数据
        orders.setId(orderId);
        orders.setNumber(String.valueOf(orderId));//订单号？
        orders.setStatus(1);//状态
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(totalAmount);
        orders.setUserName(user.getName());
        orders.setPhone(address.getPhone());
        orders.setAddress(address.getDetail());
        orders.setConsignee(address.getConsignee());

        //新增dingd
        ordersMapper.addOrders(orders);
        //批量插入订单项
        orderDetailMapper.addOrderDetail(orderDetailList);
        //F. 删除当前用户的购物车列表数据
        shoppingCartMapper.delete(userId);


    }

    /**
     * 根据用户id查找订单
     *
     * @param page
     * @param pageSize
     * @param userId
     * @return
     */
    @Override
    public Page<OrdersDto> findOrdersByUserId(Integer page, Integer pageSize, Long userId,String number,LocalDateTime startTime,LocalDateTime endTime) {
        //1.设置当前页和每页大小
        PageHelper.startPage(page,pageSize);
        //2.查询用户的所有订单
        List<OrdersDto> ordersDtoList = ordersMapper.findOrdersByUserId(userId,number,startTime,endTime);
        //3.封装PageInfo
        PageInfo<OrdersDto> ordersDtoPageInfo = new PageInfo<>(ordersDtoList);
        //封装page
        Page<OrdersDto> ordersDtoPage = new Page<>();
        ordersDtoPage.setRecords(ordersDtoPageInfo.getList());
        ordersDtoPage.setTotal(ordersDtoPageInfo.getTotal());
        return ordersDtoPage;
    }
}
